package com.chushouya.order.rabbitmq.listener;

import com.general.framework.core.exception.BaseException;
import com.general.framework.core.exception.ViolationException;
import com.chushouya.common.dto.QueueMessage;
import com.chushouya.order.service.admin.OrderBonusService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 红包核销消息监听器
 * 
 * @author chushouya
 * @date 2025-08-13
 */
@Slf4j
@Component
public class BonusMessageListener {

    @Resource
    private ObjectMapper objectMapper;
    
    @Resource
    private OrderBonusService orderBonusService;

    /**
     * 监听订单创建消息，进行红包核销
     */
    @RabbitListener(queuesToDeclare = @Queue(name = "bonus.queue"))
    public void handleBonusMessage(QueueMessage<?> queueMessage) {
        try {
                log.info("收到红包核销消息: messageId={}, data={}", queueMessage.getMessageId(), queueMessage.getData());
                
                // 验证消息数据
                if (queueMessage.getData() == null) {
                    log.error("红包核销消息数据为空: messageId={}", queueMessage.getMessageId());
                    log.warn("数据为空的消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
                    return;
                }
                
                // 处理红包核销消息
                processRabbitMessage(queueMessage);
                
                log.info("红包核销消息处理完成 - 消息ID: {}, 订单ID: {}", 
                        queueMessage.getMessageId(), queueMessage.getData());
                
            } catch (ViolationException e) {
                log.error("处理红包消息验证错误: {}, messageId: {}", e.getMessage(), queueMessage.getMessageId());
                // 验证异常不抛出，避免消息重新入队
                log.warn("验证异常消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
            } catch (BaseException e) {
                log.error("处理红包消息业务异常: {}, messageId: {}", e.getMessage(), queueMessage.getMessageId());
                // 业务异常不抛出，避免消息重新入队
                log.warn("业务异常消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
            } catch (Exception e) {
                log.error("处理红包消息异常: {}, messageId: {}", e.getMessage(), queueMessage.getMessageId(), e);
                // 其他异常不抛出，避免消息重新入队
                log.warn("异常消息已处理，避免重复处理: messageId={}", queueMessage.getMessageId());
        }
    }
    
    /**
     * 处理RabbitMQ消息
     */
    private void processRabbitMessage(QueueMessage<?> queueMessage) {
        try {
            Long orderId = Long.valueOf(queueMessage.getData().toString());
            log.info("开始处理红包核销，订单ID: {}, messageId: {}", orderId, queueMessage.getMessageId());
            
            // 调用订单红包服务处理核销逻辑
            orderBonusService.processOrderBonusVerification(orderId);
            
        } catch (Exception e) {
            log.error("处理RabbitMQ消息异常 - 消息ID: {}, 订单ID: {}, 错误: {}", 
                    queueMessage.getMessageId(), queueMessage.getData(), e.getMessage(), e);
            throw e;
        }
    }

    
}
